package com.letv.app.ljodfh.ui.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.ruihang.generalibrary.utils.ListUtils;
import com.letv.app.ljodfh.R;
import com.letv.app.ljodfh.base.BaseFragment;
import com.letv.app.ljodfh.beans.NewsItem;
import com.letv.app.ljodfh.http.AbsListNetCallback;
import com.letv.app.ljodfh.http.NetUtils;
import com.letv.app.ljodfh.ui.adapter.TopicHomeAdapter;
import com.letv.app.ljodfh.utils.ListTypeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 专题的fragment
 */
public class TopicFragment extends BaseFragment implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener {

    @BindView(R.id.can_content_view)
    protected RecyclerView recyclerView;
    @BindView(R.id.fg_topic_refresh)
    protected CanRefreshLayout refresh;

    private Unbinder unbind;

    private TopicHomeAdapter adapter;

    private int index = -1;

    public TopicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_topic, container, false);
        ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        refresh.setOnLoadMoreListener(this);
        refresh.setOnRefreshListener(this);
        refresh.setLoadMoreEnabled(false);
        refresh.autoRefresh();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbind != null) {
            unbind.unbind();
        }
        if (refresh != null) {
            refresh.setOnRefreshListener(null);
            refresh.setOnLoadMoreListener(null);
        }
    }

    @Override
    public void onLoadMore() {
        if (refresh.isRefreshing()) {
            refresh.loadMoreComplete();
            return;
        }
        getDatas(index + 1);
    }

    @Override
    public void onRefresh() {
        if (refresh.isLoading()) {
            refresh.refreshComplete();
            return;
        }
        getDatas(0);
    }

    private void getDatas(int callIndex) {
        //这里获取数据,可修改为有参数的方法
        NetUtils.getNetAdapter().getNewsPage(getOwnerName(), topicUrl, callIndex, new AbsListNetCallback<NewsItem>(ListTypeUtils.NewsItem(), callIndex) {
            @Override
            public void onSuc(List<NewsItem> newsItems) {
                setData(newsItems, calledIndex == 0);
                index = getPageResponse().getData().getPageIndex();
                refresh.setLoadMoreEnabled(getPageResponse().getData().getPageAll() > index + 1);
            }

            @Override
            public void onEnd() {
                super.onEnd();
                if (refresh.isRefreshing()) {
                    refresh.refreshComplete();
                }
                if (refresh.isLoading()) {
                    refresh.loadMoreComplete();
                }
            }
        });
    }

    private void setData(List<NewsItem> list, boolean isReset) {
        if (adapter == null) {
            if (ListUtils.isListValued(list)) {
                adapter = new TopicHomeAdapter(getContext(), getContainTitleList(list));
            } else {
                adapter = new TopicHomeAdapter(getContext(), null);
            }
            recyclerView.setAdapter(adapter);
        } else {
            if (isReset) {
                adapter.setData(getContainTitleList(list));
            } else {
                adapter.addDatasEx(list);
            }
        }
    }

    private List<NewsItem> getContainTitleList(List<NewsItem> li) {
        if (ListUtils.isListValued(li)) {
//            ArrayList<NewsItem> list = new ArrayList<>();
            NewsItem n = new NewsItem();
            n.setNewsTitle(getString(R.string.topic));
            n.setViewType(TopicHomeAdapter.TYPE_TITLE);
//            list.add(n);
            li.add(0, n);
//            list.addAll(li);
//            return list;
        }
        return li;
    }


}
